﻿Imports System.Data.Linq
Imports System.Linq
Public Class CSellingRateController
    Dim objDataContext As New TanNamChinhDataContext
    
    Public Function GetSellingRateList(ByVal idHBL As Integer) As ISingleResult(Of sp_Thanh_GetSellingRateListResult)
        Try
            Return objDataContext.sp_Thanh_GetSellingRateList(idHBL, 2)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function GetSellingRateList2(ByVal idHBL As Integer) As ISingleResult(Of sp_Thanh_GetSellingRateList_2Result)
        Try
            Return objDataContext.sp_Thanh_GetSellingRateList_2(idHBL)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Sub XoaThongTinSelling(ByVal idHBL As Integer)
        Try
            objDataContext.tblSellingFees.DeleteAllOnSubmit(From s In objDataContext.tblSellingFees Where s.ID_HBL = idHBL Select s)
            objDataContext.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Function CheckExistSellingHBL(ByVal idHBL As Integer)
        Return objDataContext.tblSellingFees.Any(Function(s) s.ID_HBL = idHBL)
    End Function
    Public Function GetSellingBillFee(ByVal _idHBL As Integer, ByVal _idFee As Integer) As tblVoyageFee
        Dim hblCtrl As New CHBLController
        Dim hbl As tblHBL = hblCtrl.SelectRecordByID(_idHBL)
        Try
            Return (From vf In objDataContext.tblVoyageFees
            Where vf.ID_Fee = _idFee And hbl.tblFileMBL.tblMBL.ID_Voyage = vf.ID_Voyage And vf.IsBuying = False
                                     Select vf).Single
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function GetSellingContFee(ByVal _idHBL As Integer, ByVal _idFee As Integer, ByVal _idContType As Integer) As tblFreightTariff
        Dim hblCtrl As New CHBLController
        Dim hbl As tblHBL = hblCtrl.SelectRecordByID(_idHBL)
        Try
            Return (From ft In objDataContext.tblFreightTariffs
                                       Where ft.ID_Fee = _idFee And hbl.tblFileMBL.tblMBL.ID_Voyage = ft.ID_Voyage And ft.IsBuying = False And ft.ID_ContType = _idContType
                                       Select ft).Single
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function GetSellingFeeList() As IQueryable(Of tblFee)
        Try
            Return (From fee In objDataContext.tblFees
                             Where fee.ID_FeeType = 2 And fee.IsActive = True
                             Select fee)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function GetSellingFeeListPlus(ByVal idHBL As Integer) As IQueryable(Of tblFee)
        Try
            Return (From fee In objDataContext.tblFees
                             Where fee.ID_FeeType = 2 And fee.IsActive = True And Not objDataContext.tblSellingFees.Any(Function(s) s.ID_HBL = idHBL And s.ID_Fee = fee.ID_Fee)
                             Select fee)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Sub TinhSellingRate(ByVal idHBL As Integer, ByVal isFCL As Boolean, ByVal sellFee As IQueryable(Of tblFee))
        Dim ctrlCont As New CContainerController
        'lay thong tin container theo hbl
        Dim contHBL = ctrlCont.SelectContsByHBL(idHBL)
        Dim contTypeHBL = ctrlCont.SelectContTypeInHBL(idHBL)
        For Each f As tblFee In sellFee
            Try
                'tinh so luong
                If f.IsBill = True Then
                    Dim newSell As New tblSellingFee
                    With newSell
                        .ID_Fee = f.ID_Fee
                        .ID_HBL = idHBL
                        .Quantity = 1
                        .UnitPrice = 0
                        .Price = 0
                        .Description = ""
                        .ID_Currency = 1
                    End With
                    Dim billFee = GetSellingBillFee(idHBL, f.ID_Fee)
                    If (billFee IsNot Nothing) Then
                        newSell.UnitPrice = billFee.Price
                        newSell.Price = billFee.Price
                        newSell.ID_Currency = billFee.ID_Currency
                    End If
                    'cập nhật vào cơ sở dữ liệu
                    If (billFee.Price.HasValue And billFee.Price > 0) Then
                        InsertRecord(newSell)
                    End If
                Else
                    Dim newSell As New tblSellingFee
                    With newSell
                        .ID_Fee = f.ID_Fee
                        .ID_HBL = idHBL
                        .Quantity = 1
                        .UnitPrice = 0
                        .Price = 0
                        .Description = ""
                        .ID_Currency = 1
                    End With
                    For Each contType In contTypeHBL
                        'số lượng cont theo contype
                        Dim contFee = GetSellingContFee(idHBL, f.ID_Fee, contType.ID_ContType)
                        If (contFee IsNot Nothing) Then
                            'nếu file là hàng lẻ thì Quantity là CBM
                            If (isFCL = False) Then
                                Dim cont = contHBL.First()
                                If (cont.CBM < 1) Then
                                    newSell.Quantity = 1
                                Else
                                    newSell.Quantity = cont.CBM
                                End If
                            Else
                                Dim soLuongCont As Integer = (From c In contHBL
                                                Where c.ID_ContType = contType.ID_ContType).Count
                                newSell.Quantity = soLuongCont
                                newSell.Description &= contType.ContTypeName & " : " & soLuongCont.ToString & "*" & contFee.Price.ToString() & ", "
                            End If
                            newSell.UnitPrice = contFee.Price
                            newSell.Price += newSell.Quantity * contFee.Price
                            newSell.ID_Currency = contFee.ID_Currency
                        End If
                    Next
                    If (newSell.Description <> "") Then
                        newSell.Description = newSell.Description.Substring(0, newSell.Description.Length - 2)
                    End If
                    If (contTypeHBL.Count > 1) Then
                        newSell.Quantity = 1
                        newSell.UnitPrice = newSell.Price
                    End If
                    If (newSell.UnitPrice.HasValue And newSell.UnitPrice > 0) Then
                        'cập nhật vào cơ sở dữ liệu
                        InsertRecord(newSell)
                    End If
                End If
            Catch ex As Exception
            End Try
        Next
    End Sub
    Public Sub ModifySellingRate(ByVal idHBL As Integer, ByVal isFCL As Boolean)
        Try
            'lay danh sach cac loai phi selling mới thêm
            Dim sellFee = GetSellingFeeListPlus(idHBL)
            TinhSellingRate(idHBL, isFCL, sellFee)
        Catch ex As Exception
        End Try
    End Sub
    Public Sub HoTroTinhSellingRate(ByVal idHBL As Integer, ByVal isFCL As Boolean)
        Try
            'xoá toàn bộ thông tin trước
            XoaThongTinSelling(idHBL)
            'lay danh sach cac loai phi selling
            Dim sellFee = GetSellingFeeList()
            TinhSellingRate(idHBL, isFCL, sellFee)
        Catch ex As Exception
        End Try
    End Sub
    Public Function SelectRecordByID(ByVal _id As Integer) As tblSellingFee
        Try
            Return (From r In objDataContext.tblSellingFees
                    Where r.ID_SellingFee = _id
                    Select r).Single
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function InsertRecord(ByVal LocalTable As tblSellingFee) As Integer
        Try
            objDataContext.tblSellingFees.InsertOnSubmit(LocalTable)
            objDataContext.SubmitChanges()
            Return LocalTable.ID_SellingFee
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Sub UpdateRecord(ByVal LocalTable As tblSellingFee)
        Try
            objDataContext.tblSellingFees.Attach(LocalTable)
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub UpdateRecordWithoutAttach(ByVal LocalTable As tblSellingFee)
        Try
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub New()
        objDataContext = New TanNamChinhDataContext
    End Sub
    Public Sub New(ByVal _obj As TanNamChinhDataContext)
        objDataContext = _obj
    End Sub
    Public Sub RefreshData()
        objDataContext = New TanNamChinhDataContext
    End Sub
End Class
